﻿Public Class frmThemLop
    Private Lop As DTOLop = New DTOLop
    Private IsAdd As Boolean = True
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Public Sub New(ByVal lop As DTOLop)

        ' This call is required by the designer.
        InitializeComponent()
        IsAdd = False
        Me.Lop = Lop
        ' Add any initialization after the InitializeComponent() call.

    End Sub


    Private Sub frmThemLop_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If IsAdd = False Then
            txtTen.Text = Me.Lop.TenLop
            txtTen.Enabled = False
            txtSiSo.Text = Me.Lop.SiSo
        End If

    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim lop As DTOLop = New DTOLop
        Try
            lop.TenLop = txtTen.Text
            lop.SiSo = Convert.ToInt64(txtSiSo.Text.ToString())
        Catch ex As Exception
            MessageBox.Show("Thông tin nhập vào bị sai.", "Lỗi.", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Return
        End Try
        
        Dim _lop As DAOLop = New DAOLop
        If IsAdd = True Then

            If _lop.LoadTable("Select * from Lop where TenLop='" + lop.TenLop.ToString() + "'", Application.StartupPath + "\database\database.mdb").Rows.Count > 0 Then
                MessageBox.Show("Tên lớp đã tồn tại.", "Thông báo.", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
                txtTen.Focus()
                Return
            End If

            If _lop.Insert(lop, Application.StartupPath + "\database\database.mdb") Then
                Dim frm As frmLop = New frmLop
                frm.Show()
                Me.Close()
            Else
                MessageBox.Show("Thêm không thành công.", "Thông báo.", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                Return

            End If
        Else            
            If _lop.Update(lop, Application.StartupPath + "\database\database.mdb") Then
                Dim frm As frmLop = New frmLop
                frm.Show()
                Me.Close()
            Else
                MessageBox.Show("Sửa không thành công.", "Thông báo.", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                Return
            End If
            
        End If

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Dim frm As frmLop = New frmLop
        frm.Show()
        Me.Close()
    End Sub
End Class